//////////////////////////////////////////////////
// guiGLWidget.cpp
//
// Author: Tarik Belabbas et Adrien Hoffer
//////////////////////////////////////////////////

// Required to use VBOs (glGenBuffers() & co)
// and must be defined BEFORE including glext.h (included by QtOpenGL)
#define GL_GLEXT_PROTOTYPES

#include <QtGui>

#include "guiGLWidget.h"
#include "../common/Common.h"
#include "../gl/glRenderer.h"

//------------------------------------------------------------------------------
//									guiGLWidget
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
guiGLWidget::guiGLWidget()
{
	m_frames		= 0;

	// A timer event will occur every "interval" milliseconds until killTimer()
	// is called. If interval is 0, then the timer event occurs once every time
	// there are no more window system events to process.
	startTimer( 1000 / 60 ); // 60 FPS

	// The value that is returned indicates the first error that occurred since
	// startup or since the last call to glGetError()
	//GLenum glGetError();

	// 1.5 Mesa 7.8.2
	//const GLubyte* string = glGetString( GL_VERSION );

	setFocusPolicy( Qt::StrongFocus );
}

//------------------------------------------------------------------------------
guiGLWidget::~guiGLWidget()
{
	makeCurrent(); // ?
}

//------------------------------------------------------------------------------
QSize guiGLWidget::minimumSizeHint() const
{
	return QSize( 50, 50 );
}

//------------------------------------------------------------------------------
QSize guiGLWidget::sizeHint() const
{
	return QSize( 1920, 1080 );
}

//------------------------------------------------------------------------------
void guiGLWidget::initializeGL()
{
	glRenderer::s_getInstance()->initialize();
}

//------------------------------------------------------------------------------
void guiGLWidget::paintGL()
{
	glRenderer::s_getInstance()->render();
}

//------------------------------------------------------------------------------
void guiGLWidget::resizeGL( int _width, int _height )
{
	glRenderer::s_getInstance()->resizeViewport( _width, _height );
}

//------------------------------------------------------------------------------
void guiGLWidget::timerEvent( QTimerEvent* /*_event*/ )
{
//	m_camera->updateMove();

	updateGL();
}

//------------------------------------------------------------------------------
void guiGLWidget::mousePressEvent( QMouseEvent* _event )
{
	setMouseTracking( true );
	m_lastPos = _event->pos();
}

//------------------------------------------------------------------------------
void guiGLWidget::mouseMoveEvent( QMouseEvent* _event )
{
	int deltaX = _event->x() - m_lastPos.x();
	int deltaY = _event->y() - m_lastPos.y();

	m_lastPos = _event->pos();

	if ( deltaX != 0 )
	{
		float rotationAngleX = 0.5F * ( float )deltaX;
		glRenderer::s_getInstance()->rotateCameraX( rotationAngleX );
	}

	if ( deltaY != 0 )
	{
		float rotationAngleY = 0.5F * ( float )deltaY;
		glRenderer::s_getInstance()->rotateCameraY( rotationAngleY );
	}

	m_lastPos = _event->pos();
}

//------------------------------------------------------------------------------
void guiGLWidget::wheelEvent( QWheelEvent* /*_event*/ )
{
	/*GLfloat viewport[ 4 ];
	glGetFloatv( GL_VIEWPORT, viewport );

	if ( _event->orientation() == Qt::Vertical )
	{
		m_camera->changeZoom( _event->delta(), viewport[ 2 ]/viewport[ 3 ] );

		updateGL();
	}

	_event->accept();*/
}

//------------------------------------------------------------------------------
void guiGLWidget::keyPressEvent( QKeyEvent* _event )
{
    switch ( _event->key() )
    {
    	case Qt::Key_Z:
    		glRenderer::s_getInstance()->enableCamFlag( glECameraMovementFlag_FORWARD );
    		break;

    	case Qt::Key_S:
    		glRenderer::s_getInstance()->enableCamFlag( glECameraMovementFlag_BACKWARD );
			break;

    	case Qt::Key_Q:
    		glRenderer::s_getInstance()->enableCamFlag( glECameraMovementFlag_LEFT );
    		break;

    	case Qt::Key_D:
    		glRenderer::s_getInstance()->enableCamFlag( glECameraMovementFlag_RIGHT );
    		break;

    	case Qt::Key_Shift:
    		glRenderer::s_getInstance()->enableCamFlag( glECameraMovementFlag_SPEEDX10 );
    	    break;

    	case Qt::Key_Control:
    		glRenderer::s_getInstance()->enableCamFlag( glECameraMovementFlag_SPEEDX100 );
			break;

    	default:
    		break;
    }
}

//------------------------------------------------------------------------------
void guiGLWidget::keyReleaseEvent( QKeyEvent* _event )
{
    switch ( _event->key() )
    {
    	case Qt::Key_Z:
    		glRenderer::s_getInstance()->disableCamFlag( glECameraMovementFlag_FORWARD );
			break;

		case Qt::Key_S:
			glRenderer::s_getInstance()->disableCamFlag( glECameraMovementFlag_BACKWARD );
			break;

		case Qt::Key_Q:
			glRenderer::s_getInstance()->disableCamFlag( glECameraMovementFlag_LEFT );
			break;

		case Qt::Key_D:
			glRenderer::s_getInstance()->disableCamFlag( glECameraMovementFlag_RIGHT );
			break;

		case Qt::Key_Shift:
			glRenderer::s_getInstance()->disableCamFlag( glECameraMovementFlag_SPEEDX10 );
			break;

		case Qt::Key_Control:
			glRenderer::s_getInstance()->disableCamFlag( glECameraMovementFlag_SPEEDX100 );
			break;

		case Qt::Key_O:
			glRenderer::s_getInstance()->moveCameraTo( 0.0F, 0.0F, 0.0F );
			break;

		case Qt::Key_Escape:
			setMouseTracking( false );
			break;

    	default:
    		break;
    }
}

//------------------------------------------------------------------------------
void guiGLWidget::focusInEvent( QFocusEvent* /*_event*/ )
{
	//qDebug() << "focus in !";
	//showFullScreen();
}

//------------------------------------------------------------------------------
void guiGLWidget::focusOutEvent( QFocusEvent* /*_event*/ )
{
	//qDebug() << "focus out !";
}
